package com.CampaignManager.shared;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("serial")
public class UnitDTO implements Serializable {
	
	private Long id;
	private String ownerEmail;
	private String name;
	private Long points;
	private UnitTypeDTO unitType;
	private Date lastPaid;
		
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getOwnerEmail() {
		return ownerEmail;
	}
	public void setOwnerEmail(String ownerEmail) {
		this.ownerEmail = ownerEmail;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Long getPoints() {
		return points;
	}
	public void setPoints(Long points) {
		this.points = points;
	}
	public UnitTypeDTO getUnitType() {
		return unitType;
	}
	public void setUnitType(UnitTypeDTO unitType) {
		this.unitType = unitType;
	}	
	public Date getLastPaid() {
		return lastPaid;
	}
	public void setLastPaid(Date lastPaid) {
		this.lastPaid = lastPaid;
	}
	
	public Long getUpgradePointValue() {
		Long pointsAway = 0L;
		if (this.getPoints() < this.getUnitType().getMinPoints()) {
			pointsAway = this.getUnitType().getMinPoints() - this.getPoints();
		} else if (this.getPoints() < this.getUnitType().getMaxPoints()) {
			pointsAway = this.getUnitType().getMaxPoints() - this.getPoints();
		}
		return pointsAway;
	}
	public List<ResourceAmountDTO> getUpgradeCost(String faction) {
		
		List<ResourceAmountDTO> racs = new ArrayList<ResourceAmountDTO>();
		
		Long pointsAway = this.getUpgradePointValue();
		
		if (pointsAway <= 0) return racs;
		
		boolean isMerc = this.getUnitType().isMerc(faction);
		if (isMerc) {
			ResourceAmountDTO r = new ResourceAmountDTO();
			r.setResource("Gold");
			r.setAmount(pointsAway*10);
			racs.add(r);
		} else {
			
			double pctNeeded = (double)pointsAway / (double)this.getUnitType().getMinPoints();
			
			for (ResourceAmountDTO rac : this.getUnitType().getResources()) {
				ResourceAmountDTO r = new ResourceAmountDTO();
				r.setResource(rac.getResource());
				r.setAmount(Math.round(rac.getAmount()* pctNeeded));
				racs.add(r);
			}
		}
		return racs; 
	}
}
